<template>
  <div v-loading.lock="loading" class="base-content">
    <div class="basic-info">
      <el-form ref="queryForm" :model="queryForm" inline>
        <el-form-item label="样品编号" prop="sampleNum">
          <el-input
            v-model="queryForm.sampleNum"
            placeholder="样品编号"
            clearable
            @keyup.enter.native="onQuery()"
          />
        </el-form-item>
        <el-form-item label="委托单号" prop="orderNum">
          <el-input
            v-model="queryForm.orderNum"
            placeholder="委托单号"
            clearable
            @keyup.enter.native="onQuery()"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onQuery">查询</el-button>
          <el-button @click="cancelForm">清空</el-button>
        </el-form-item>
      </el-form>
      <!--操作栏-->
      <div class="base-content-operate">
        <el-button type="primary" icon="el-icon-plus" @click="pooledDetection"
          >合并检测</el-button
        >
        <!-- <el-button
            type="primary"
            icon="el-icon-document-copy"
            @click="entrustTaskDistributionFun"
            >导出</el-button
          >

          <el-button v-if="false"
          icon="el-icon-document-copy"
          type="primary"
          @click="importData"
          >导入</el-button> -->
      </div>
      <!--列表-->
      <el-table
        :data="list"
        tooltip-effect="dark"
        height="calc(100% - 150px)"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column align="center" label="序号" width="50">
          <template slot-scope="scope">
            {{ scope.$index + 1 }}
          </template>
        </el-table-column>
        <el-table-column label="委托单号">
          <template slot-scope="scope">
            {{ scope.row.entrustOrderNum }}
          </template>
        </el-table-column>
        <el-table-column v-if="systemType == 'cdc'" label="委托科室">
          <template slot-scope="scope">
            {{ scope.row.departmentName }}
          </template>
        </el-table-column>
        <el-table-column v-if="systemType == 'cdc'" label="被采样单位/地点">
          <template slot-scope="scope">
            {{ scope.row.samplingUnitPosition }}
          </template>
        </el-table-column>
        <el-table-column label="样品编号">
          <template slot-scope="scope">
            {{ scope.row.sampleNum }}
          </template>
        </el-table-column>
        <el-table-column label="样品名称">
          <template slot-scope="scope">
            {{ scope.row.sampleName }}
          </template>
        </el-table-column>
        <el-table-column label="检测项目数量">
          <template slot-scope="scope">
            {{ scope.row.detectionItemCount }}
          </template>
        </el-table-column>
        <el-table-column label="样品接收时间">
          <template slot-scope="scope">
            {{ scope.row.sampleAcceptTime }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="140">
          <template slot-scope="scope">
            <div class="base-table-operation">
              <el-link
                type="primary"
                @click="entrustTaskDistributionFun(scope.row)"
                >任务分配</el-link
              >
            </div>
          </template>
        </el-table-column>
      </el-table>

      <BasePagination
        :total="pagination.total"
        :page-index="pagination.pageIndex"
        :page-size="pagination.pageSize"
        :page-count="pageCount"
        @sizeChange="sizeChange"
        @changePage="currentChange"
      />
    </div>
    <EntrustTaskAllocationDialog
      v-if="dialogFormAllocationVisible"
      ref="EntrustTaskAllocationDialog"
      :dialog-form-allocation-visible.sync="dialogFormAllocationVisible"
      :task-data.sync="taskData"
      @getList="getList"
    />
    <PooledDetectionDialog
      v-if="dialogFormPooledDetectionVisible"
      ref="PooledDetectionDialog"
      :dialog-form-pooled-detection-visible.sync="
        dialogFormPooledDetectionVisible
      "
      :entrust-order-sample-registration-id-list.sync="
        entrustOrderSampleRegistrationIdList
      "
      :entrust-task-id-list.sync="entrustTaskIdList"
      @getList="getList"
    />
  </div>
</template>
<script>
import {
  deleteRecord,
  TaskAllocation,
} from "../../../api/EntrustTaskManage/EntrustTask.js";
import { getConfigByKey } from "../../../api/System/SystemDict";
import PooledDetectionDialog from "../../../components/EntrustTaskManage/PooledDetectionDialog.vue";
import EntrustTaskAllocationDialog from "../../../components/EntrustTaskManage/EntrustTaskAllocationDialog.vue";
export default {
  name: "EntrustTaskList",
  components: { PooledDetectionDialog, EntrustTaskAllocationDialog },
  data() {
    return {
      entrustTaskIdList: [],
      systemType: "",
      taskData: {},
      dialogFormAllocationVisible: false,
      tableDatas: [],
      dialogFormPooledDetectionVisible: false,
      loading: true,
      list: null,
      queryForm: {
        orderNum: "",
        sampleNum: "",
        detectionItemName: "",
        sampleRegistrationId: 0,
      },
      pagination: {
        pageIndex: 1,
        pageSize: 10,
        total: 0,
      },
      pageCount: 0,
      entrustOrderSampleRegistrationIdList: [],
    };
  },
  created() {
    // 初始化数据
    if (this.$route.query.orderNum !== undefined) {
      this.queryForm.orderNum = this.$route.query.orderNum;
    }
    if (this.$route.query.sampleRegistrationId) {
      this.queryForm.sampleRegistrationId =
        this.$route.query.sampleRegistrationId;
    }
    // 查询字典表判断系统类型
    getConfigByKey("SystemType").then((res) => {
      this.systemType = res.data[0].value;
    });

    this.getList("filter");
  },
  methods: {
    // 合并检测
    pooledDetection() {
      if (this.tableDatas === null || this.tableDatas.length === 0) {
        this.$message.error("请选择数据");
        return false;
      }
      this.entrustOrderSampleRegistrationIdList = [];
      this.entrustTaskIdList = [];
      for (let i = 0; i < this.tableDatas.length; i++) {
        this.entrustOrderSampleRegistrationIdList.push(this.tableDatas[i].id);
      }
      this.dialogFormPooledDetectionVisible = true;
    },

    currentChange(val) {
      this.pagination.pageIndex = val;
      this.getList();
    },
    sizeChange(val) {
      this.pagination.pageIndex = 1;
      this.pagination.pageSize = val;
      this.getList("filter");
    },
    getList(flag) {
      if (flag) {
        this.pagination.pageIndex = 1;
      }
      this.list = [];
      this.loading = true;

      TaskAllocation(this.queryForm, this.pagination)
        .then((res) => {
          this.list = res.data.dataList ? res.data.dataList : [];
          this.pagination.total = res.data.total;
          this.loading = false;
        })
        .catch(() => {
          this.loading = false;
        });
    },
    entrustTaskDistributionFun(data) {
      this.taskData = data;
      this.dialogFormAllocationVisible = true;
    },
    handleDelete(rowData) {
      this.Helper.deleteConfirm(() => {
        this.loading = true;
        deleteRecord({ id: rowData.id })
          .then((res) => {
            this.getList();
            this.loading = false;
          })
          .catch(() => {
            this.loading = false;
          });
      });
    },

    onQuery() {
      this.getList();
    },

    // 清空筛选条件
    cancelForm() {
      this.$refs.queryForm.resetFields();
      this.getList();
    },
    // 选择列表
    handleSelectionChange(val) {
      this.tableDatas = val;
    },
  },
};
</script>
